package com.chenwei.rabbit.routingTopics;

import com.chenwei.rabbit.MQUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;

/**
 * @author: chenwei
 * @date: 2021/4/1 10:37
 */
public class Customer2 {
    public static void main(String[] args) throws IOException {
        Connection connection = MQUtils.getConnection();
        Channel channel = connection.createChannel();
        //通道绑定 交换机
       channel.exchangeDeclare("routingTopic", "topic");
        //获取队列
        String queueName  = channel.queueDeclare().getQueue();
        //基于routingKey绑定交换机和队列 参数1：队列名称 参数2：交换机名称 参数3：路由key
        channel.queueBind(queueName , "routingTopic", "aa.#");
        //消费消息  参数2 自动确认消息
        channel.basicConsume(queueName, true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("customer1："+new String(body));
            }
        });
    }
}
